{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "import * only allowed at module level (cell_name, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"cell_name\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m import * only allowed at module level\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from dataset import MNISTDataset\n",
    "from model import *\n",
    "\n",
    "from scipy.spatial.distance import cdist\n",
    "from matplotlib import gridspec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===Loading MNIST Dataset===\n",
      "Images train : (60000, 28, 28, 1)\n",
      "Labels train : (60000, 1)\n",
      "Images test  : (10000, 28, 28, 1)\n",
      "Labels test  : (10000,)\n",
      "Unique label : [0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "dataset = MNISTDataset()\n",
    "train_images = dataset.images_train[:20000]\n",
    "test_images = dataset.images_test\n",
    "len_test = len(train_images)\n",
    "len_train = len(test_images)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#helper function to plot image\n",
    "def show_image(idxs, data):\n",
    "    if type(idxs) != np.ndarray:\n",
    "        idxs = np.array([idxs])\n",
    "    fig = plt.figure()\n",
    "    gs = gridspec.GridSpec(1,len(idxs))\n",
    "    for i in range(len(idxs)):\n",
    "        ax = fig.add_subplot(gs[0,i])\n",
    "        ax.imshow(data[idxs[i],:,:,0])\n",
    "        ax.axis('off')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create the siamese net feature extraction model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_placeholder = tf.placeholder(tf.float32, [None, 28, 28, 1], name='img')\n",
    "net = mnist_model(img_placeholder, reuse=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Restore from checkpoint and calc the features from all of train data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from model/model.ckpt\n"
     ]
    }
   ],
   "source": [
    "saver = tf.train.Saver()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    ckpt = tf.train.get_checkpoint_state(\"model\")\n",
    "    saver.restore(sess, \"model/model.ckpt\")\n",
    "    \n",
    "    train_feat = sess.run(net, feed_dict={img_placeholder:train_images[:10000]})                "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Searching for similar test images from trainset based on siamese feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#generate new random test image\n",
    "idx = np.random.randint(0, len_test)\n",
    "im = test_images[idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "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\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is image from id: 9522\n"
     ]
    }
   ],
   "source": [
    "#show the test image\n",
    "show_image(idx, test_images)\n",
    "print(\"This is image from id:\", idx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from model/model.ckpt\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXMAAABLCAYAAACC5GfmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAAEmRJREFUeJzt3Xd8VNW2wPHfzCQkIaSHnoSSLjV0xKhYkUsVxWu5IlIEnsgDbxSfKIKIPrHQRel8UJp0UORSpBNAE0QChIRejBAgQHpm5v2xkwkhHTKTyXnr+w+TOeeEPZkz6+y9ztp7dGazGSGEEFWbvrIbIIQQ4v5JMBdCCA2QYC6EEBogwVwIITRAgrkQQmiABHMhhNAACeZCCKEBEsyFEEIDJJgLIYQGONjyP3tS/7zdTzf9j2mFrqTt8hpsQ16DfZDXYB9Kew0gPXMhhNAECeZCCKEBEsyFEEIDJJgLIYQGSDAXQggNkGAuhBAaIMFcCCE0QIK5EEJogE0nDZXkl0uxAGSbjZbnuvfsh/nQn5XVJCGEKGTp+b08NPPfADSYl4Ax6e9KbpFiN8E8L4ibMFme+2blLLIpeuJTnxlR1Pt8r03adj8Mnh6cmNYYgOOPzSm0PWTDEAACNuhwXn/Apm0TirlTS9I+uAlA+qraeJ3MBOB0j2p4HlPnn+93+yqtfXmyn2jNlZZOAOgeul5gm3m3FzVjVbudz17HePJUgeMAwj49yi+7WwIQNHK/LZqsSe56Z2LfnAbA6SEZ9JwfBUDAR5UbjyTNIoQQGmA3PfOi1HNwKnbb98O+4vxgTwAcMTJk4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mx/vdmiqrHo2sC6ZXdiApUZ8penjBHWU722nHaHnlbg85sllAhhBBVnaRZhBBCAySYCyGEBkgwF0IIDZBgLoQQGiDBXAghNECCuRBCaIAEcyGE0AAJ5kIIoQESzIUQQgMkmAshhAZIMBdCCA2QYC6EEBogwVwIITRAgrkQQmiABHMhhNAACeZCCKEBEsyFEEIDJJgLIYQGSDAXQggNkGAuhBAaIMFcCCE0QIK5EEJogARzIYTQgP8D4Mpqx2E94FUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 7 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "retrieved ids: [1480 4334  933 6889 7616 3579 7386]\n"
     ]
    }
   ],
   "source": [
    "#run the test image through the network to get the test features\n",
    "saver = tf.train.Saver()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    ckpt = tf.train.get_checkpoint_state(\"model\")\n",
    "    saver.restore(sess, \"model/model.ckpt\")\n",
    "    search_feat = sess.run(net, feed_dict={img_placeholder:[im]})\n",
    "    \n",
    "#calculate the cosine similarity and sort\n",
    "dist = cdist(train_feat, search_feat, 'cosine')\n",
    "rank = np.argsort(dist.ravel())\n",
    "\n",
    "#show the top n similar image from train data\n",
    "n = 7\n",
    "show_image(rank[:n], train_images)\n",
    "print(\"retrieved ids:\", rank[:n])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
